using SmartCommunity.WebApi.Services.System;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.WebApi.Controllers.System
{
    /// <summary>
    /// API接口：字典明细
    /// </summary>
    [ApiController]
    [Route("api/system/[controller]")]
    public class DictItemController : BaseController
    {
        #region 构造函数
        private readonly DictItemService dictItemService;
        public DictItemController(DictItemService dictItemService)
        {
            this.dictItemService = dictItemService;
        }
        #endregion

        #region 查询导出         

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<DictItemDto> GetOne(int id)
        {
           return await dictItemService.FindDictItemById(id);
        }
        
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增字典明细
        /// </summary>
        [HttpPost]
        [Log("新增字典明细", OperateType.Create, false, true)]
        public async Task<ActionResult<DictItemDto>> Create([FromBody] DictItemDto dto)
        {
            var dictItem = await dictItemService.CreateDictItem(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = dictItem.DictItemId }, dictItem);
        }

        /// <summary>
        /// 修改字典明细
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改字典明细", OperateType.Update)]
        public async Task<ActionResult> Update(int id, [FromBody] DictItemDto dto)
        {
            if (id != dto.DictItemId)
            {
                return WarningMessage("要更新的数据和ID不匹配");
            }
            await dictItemService.UpdateDictItem(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除字典明细
        /// </summary>
        [HttpDelete]
        [Log("删除字典明细", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] int[] ids)
        {
            await dictItemService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
